/*
 * @description: 
 * @Author: latiao
 * @Date: 2022-01-05 13:51:16
 * @LastEditors: latiao
 * @LastEditTime: 2022-09-06 11:40:13
 */
import Bxios from "./bxios";
import store from "@/store";
import errorHandler from "./errorHandler";

const excludesUrl = [
  "/shipping/get/point/note/now",
  "/shipping/get/point/note/history",
  "/shops/renovation/template/def/one",
];

const assmbleUrl = [
  "/assemble/association/ass_point",
  "/assemble/association/ass",
  "/assemble/partner/admin/send_code",
  "/assemble/partner/admin/check_message_code",
  "/assemble/activity/ass/list/product_show_category",
];

const bxios = new Bxios({ baseUrl: process.env.VUE_APP_BASE_URL });

bxios.interceptors.request.use(
  conf => {
    const { token} = store.state.userStore.userInfo ;
    const { tenantId} = store.state.userStore.userInfo ;
    Object.assign(conf.header, {
      'Access-Token':token ,
      tenantId:tenantId,
      version: process.env.VUE_APP_APP_REQUEST_VERSION,
      Platform:process.env.VUE_APP_PLAT,
      'Auth-Platform':process.env.VUE_APP_AUTH_PLATFORM,
      shopId:tenantId+'100001',
      // process.env.VUE_APP_SHOPID
    });
    return conf;
  },
  err => {
    return Promise.reject(err);
  },
);

bxios.interceptors.response.use<any>(
  res => {
    if (assmbleUrl.find(i => res.config.url.indexOf(i) !== -1)) {
      return res;
    }
    if (res.data.code === 200) {
      if (excludesUrl.find(i => res.config.url.indexOf(i) !== -1)) {
        return res.data;
      } else {
        return res.data.data;
      }
    }
    return Promise.reject(errorHandler(res.data));
  },
  err => {
    return Promise.reject(err);
  },
);

export default bxios;
